package com.kayt.cyberBar.common.json;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.util.StdDateFormat;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Kevin
 * @version 1.0
 * @File javacommons.json
 * @CreateDate 10/9/2016 19:25
 * @Description <p>此类直接将Null属性转换为空字符串</p>
 */
public class JsonMapper extends ObjectMapper {
    public JsonMapper(){
        super();
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                jgen.writeString("");
            }
        });
        configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS,true);
        configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES,true);
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }
}
